import * as apis from '../services/searchService.js'

export default {
    namespace: 'search',

    state: {
        finishedTechnologyList:[],
        technologyDetailedInformation:{},
        otherTechnology:[]
    },

    reducers: {
        setFinishedTechnologyList:setFinishedTechnologyList,
        setTechnologyDetailedInformation:setTechnologyDetailedInformation,
        setOtherTechnology:setOtherTechnology
    },

    effects: {
        getFinishedTechnologyTableData: getFinishedTechnologyTableData,
        searchDetailedInformationByTechnologyId:searchDetailedInformationByTechnologyId,
        getOtherTechnologyByUserId:getOtherTechnologyByUserId
    }
}

function setOtherTechnology(state,payload){
    let _state = JSON.parse(JSON.stringify(state));
    _state.otherTechnology = payload.data;
    return _state
}

function setFinishedTechnologyList(state,payload){
    let _state = JSON.parse(JSON.stringify(state));
    _state.finishedTechnologyList = payload.data;
    return _state
}

function setTechnologyDetailedInformation(state,payload){
    let _state = JSON.parse(JSON.stringify(state));
    _state.technologyDetailedInformation = payload.data;
    return _state
}

/**
 * 根据技术编号查看相关供应商及技术详细信息
 */
function* searchDetailedInformationByTechnologyId(payload,{select,call,put}){
    let result = yield call(apis.searchDetailedInformationByTechnologyId,payload.data)
    for(let key in result.data.data){
        if(key == "functionalMaterials"){
            if(result.data.data[key] == "1"){
                result.data.data[key] = "生活垃圾"
            }
            if(result.data.data[key] == "2"){
                result.data.data[key] = "农业废物"
            }
            if(result.data.data[key] == "3"){
                result.data.data[key] = "工业固体废物"
            }
            if(result.data.data[key] == "4"){
                result.data.data[key] = "危险废弃物"
            }
            if(result.data.data[key] == "5"){
                result.data.data[key] = "其他"
            }
        }

        if(key == "technologyCategory"){
            if(result.data.data[key] == "1"){
                result.data.data[key] = "固废分离技术"
            }
            if(result.data.data[key] == "2"){
                result.data.data[key] = "循环再利用技术"
            }
            if(result.data.data[key] == "3"){
                result.data.data[key] = "减少污染"
            }
            if(result.data.data[key] == "4"){
                result.data.data[key] = "分类回收"
            }
            if(result.data.data[key] == "5"){
                result.data.data[key] = "其他"
            }
        }

        if(key == "remainingValidDate"){
            let time = result.data.data[key].substring(0,10)
            result.data.data[key] = time
        }
    }

    yield put({
        type:"search/getOtherTechnologyByUserId",
        data:{userId:result.data.data.userId}
    })

    yield put({
        type:"search/setTechnologyDetailedInformation",
        data:result.data.data
    })

  
}

/**
 * 查询当前技术的供应商的其他技术
 */
function* getOtherTechnologyByUserId(payload,{select,call,put}){
    let result = yield call(apis.getOtherTechnologyByUserId,payload.data)
    let {technologyDetailedInformation} = yield select(state=>state.search)
    for(let i=0;i<result.data.data.length;i++){
        if(result.data.data[i].id == technologyDetailedInformation.id){
            result.data.data.splice(i,1)
        }
    }
    
    yield put({
        type:"search/setOtherTechnology",
        data:result.data.data
    })
}

/**
 * 获取所有已完成验证的技术列表
 */
function* getFinishedTechnologyTableData(payload, { select, call, put }){
    let result = yield call(apis.getFinishedTechnologyTableData)
    for(let i = 0;i<result.data.data.length;i++){
        for(let key in result.data.data[i]){
            if(key == "functionalMaterials"){
                if(result.data.data[i][key] == "1"){
                    result.data.data[i][key] = "生活垃圾"
                }
                if(result.data.data[i][key] == "2"){
                    result.data.data[i][key] = "农业废物"
                }
                if(result.data.data[i][key] == "3"){
                    result.data.data[i][key] = "工业固体废物"
                }
                if(result.data.data[i][key] == "4"){
                    result.data.data[i][key] = "危险废弃物"
                }
                if(result.data.data[i][key] == "5"){
                    result.data.data[i][key] = "其他"
                }
            }
    
            if(key == "technologyCategory"){
                if(result.data.data[i][key] == "1"){
                    result.data.data[i][key] = "固废分离技术"
                }
                if(result.data.data[i][key] == "2"){
                    result.data.data[i][key] = "循环再利用技术"
                }
                if(result.data.data[i][key] == "3"){
                    result.data.data[i][key] = "减少污染"
                }
                if(result.data.data[i][key] == "4"){
                    result.data.data[i][key] = "分类回收"
                }
                if(result.data.data[i][key] == "5"){
                    result.data.data[i][key] = "其他"
                }
            }
    
            if(key == "remainingValidDate"){
                let time = result.data.data[i][key].substring(0,10)
                result.data.data[i][key] = time
            }
        }
    }

    yield put({
        type:"search/setFinishedTechnologyList",
        data:result.data.data
    })
}
